home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / 第1特集Plug-in / Photoshop / Marhil plugin 1.4.sit / Marhil plugin 1.4 / Source Code / Utilities.c < prev    next >
C/C++ Source or Header  |  1994-02-15  |  3KB  |  139 lines

  1. #include <Types.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "Utilities.h"
  5. #include "math.h"
  6.  
  7. typedef short double *psdouble, **ppsdouble;
  8.  
  9. OSErr dimimgr(Pimgdr m, int y, int x){
  10.     int i;
  11.     m->y = y;
  12.     m->x = x;
  13.     if (!(m->e = (ppsdouble) NewPtr(sizeof(psdouble) * (long)y))) return memFullErr;
  14.     for (i=0; i<y; i++){
  15.         if (!(m->e[i] = (psdouble)NewPtr(sizeof(short double) * (long)x))){
  16.             m->y = i;
  17.             freeimgr(m);
  18.             return memFullErr;
  19.         }
  20.     }
  21.     return noErr;    
  22. }
  23.  
  24. void freeimgr(Pimgdr m){
  25.     int y,i;
  26.     y = m->y;
  27.     for (i=0; i<y; i++)
  28.         DisposPtr((Ptr)m->e[i]);
  29.     DisposPtr((Ptr)m->e);
  30. }
  31.  
  32. int GetDialogItem(DialogPtr TheDialog, int item){
  33.     short ItemType;
  34.     Rect ItemBox; 
  35.       Handle ItemHdl; 
  36.     Str255 str;
  37.     
  38.     GetDItem(TheDialog, item, &ItemType, &ItemHdl, &ItemBox);
  39.     return GetCtlValue((ControlHandle)ItemHdl);
  40. }
  41.  
  42. void SetDialogItem(DialogPtr TheDialog, int item, int n){
  43.     short ItemType;
  44.     Rect ItemBox; 
  45.     Handle ItemHdl; 
  46.     Str255 str;
  47.   
  48.     GetDItem(TheDialog, item, &ItemType, &ItemHdl, &ItemBox);
  49.     SetCtlValue((ControlHandle)ItemHdl, n);
  50. }
  51.  
  52. void SetDNum(DialogPtr TheDialog, int item, int n){
  53.     short ItemType;
  54.     Rect ItemBox; 
  55.     Handle ItemHdl; 
  56.     Str255 str;
  57.   
  58.       GetDItem(TheDialog, item, &ItemType, &ItemHdl, &ItemBox);
  59.     NumToString(n,str);
  60.     SetIText(ItemHdl, str);
  61. }
  62.  
  63. void SetDReal(DialogPtr TheDialog, int item, double n, int fwidth){
  64.       short ItemType;
  65.       Rect ItemBox; 
  66.       Handle ItemHdl; 
  67.       Str255 str;
  68.  
  69.       GetDItem(TheDialog, item, &ItemType, &ItemHdl, &ItemBox);
  70.      Real2String(n,1, fwidth, str);
  71.      SetIText(ItemHdl, str);
  72. }
  73.  
  74. double GetDReal(DialogPtr TheDialog, int item){
  75.       Str255 str;
  76.     Rect r;
  77.     short itemType;
  78.     Handle Text;
  79.     
  80.     GetDItem (TheDialog, item, &itemType, &Text, &r);
  81.     GetIText (Text, str);
  82.       return String2Real(str);
  83. }
  84.  
  85. int GetDNum(DialogPtr TheDialog, int item){
  86.       long n;
  87.       Str255 str;
  88.     Rect r;
  89.     short itemType;
  90.     Handle Text;
  91.     
  92.     GetDItem (TheDialog, item, &itemType, &Text, &r);
  93.     GetIText (Text, str);
  94.       StringToNum(str,&n);
  95.       return n;
  96. }
  97.  
  98. void SetDString(DialogPtr TheDialog, int item, Str255 str){
  99.       short ItemType;
  100.       Rect ItemBox; 
  101.       Handle ItemHdl; 
  102.  
  103.       GetDItem(TheDialog, item, &ItemType, &ItemHdl, &ItemBox);
  104.       SetIText(ItemHdl, str);
  105. }
  106.  
  107. void GetDString(DialogPtr TheDialog, int item, Str255 str){
  108.     Rect r;
  109.     short itemType;
  110.     Handle Text;
  111.     
  112.     GetDItem (TheDialog, item, &itemType, &Text, &r);
  113.     GetIText (Text, str);
  114. }
  115.  
  116. double String2Real(Str255 str){
  117.     PtoCstr(str);
  118.       return atof((char *)str);
  119. }
  120.  
  121.  
  122.  void Real2String(double val, int width, int fwidth, Str255 str)/* ignoring width for now */
  123. {
  124.       char fs[] = "%6.2f";
  125.       /* fwidth =-1 => make up a sensible value for fwidth  0 2 or 4 */
  126.       if ( fwidth < 0 ) {
  127.         if ( val < 1.0 )
  128.              fwidth = 4;
  129.         else if ( (int)val == val )
  130.              fwidth = 0;
  131.         else
  132.              fwidth = 2;
  133.       }
  134.       fs[1] = '0'+width;
  135.       fs[3] = '0'+fwidth;
  136.       sprintf((char *)str,fs,val);
  137.       CtoPstr((char *)str);
  138.  }
  139.